﻿<?php 
date_default_timezone_set('PRC');
header('content-type:text/html;charset=utf-8');
//1.包含所需文件
error_reporting(0);
require_once '../swiftmailer-master/lib/swift_required.php';
require_once '../model/PdoMySQL.class.php';
require_once '../model/config.php';
require_once '../model/pwd.php';


//开启session
if(!isset($_SESSION)){
	session_start();
}
//2.接收信息
$act=$_GET['act'];
$identify=$_POST['usertype'];
$username=addslashes($_POST['username']);
$password=base64_encode($_POST['password']);
$email=addslashes($_POST['email']);
$status='0';
$usertype=addslashes($_POST['usertype']);
if($usertype=='科研人员'){
	$usertype=1;
}else if($usertype=='兽医'){
	$usertype=2;
}else{
	$usertype=3;
	}

$projectername=addslashes($_POST['projectername']);
$applyer=addslashes($_POST['applyer']);
$phone=addslashes($_POST['phone']);
$mobilephone=addslashes($_POST['mobilephone']);
$QQ=addslashes($_POST['QQ']);
 if($identify=='科研人员'){
		$table='projector';
	}else if($identify=='兽医'){
	$table='doctor';
}else if($identify=='工作人员'){
	$table='worker';
}
$table1='administrator';
//3.得到连接对象
$PdoMySQL=new PdoMySQL();
if($act==='reg'){
	//$regtime= date('Y-m-d H:i:s');
	$data=compact('username','password','email','status','usertype','projectername','applyer','phone','mobilephone','QQ');
	$res=$PdoMySQL->add($data, $table);
	$lastInsertId=$PdoMySQL->getLastInsertId();
	if($res){
		//echo $username.'<br/>'.$password.'<br/>'.$email.'<br/>'.$status.'<br/>'.$usertype.'<br/>'.$projectername.'<br/>'.						             $applyer.'<br/>'.$phone.'<br/>'.$mobilephone.'<br/>'.$QQ;
		echo'<script language="JavaScript">window.alert("申请信息已经提交，等待管理员审核"),location.href="../view/login.php";</script>';
	}
}elseif($act==='login'){
//session开始，存储用户信息

	$_SESSION['loginuser'] = $username;
	//管理员登录
	if($identify=='管理员'){
		echo '管理员';
		$pdo=new PDO('mysql:host=localhost;dbname=db_pet','root','root');
		$sql="select * from ".$table1." where username=? and password=?";
		$stmt=$pdo->prepare($sql);
		$stmt->execute(array($username,$password));
		//echo $username.'<hr/>'.$password;
		$row1=$stmt->fetch();
		if($row1['id']==1){
			echo "<script>window.location.href=\"../admin/account.php\";</script>";
		}else{
			echo'<script language="JavaScript">window.alert("账号或密码错误"),location.href="../view/login.php";</script>';
		}
		
	}else if($identify=='科研人员'){
		$pdo=new PDO('mysql:host=localhost;dbname=db_pet','root','root');
		$sql2="select * from ".$table." where username=? and password=?";
		$stmt2=$pdo->prepare($sql2);
		$stmt2->execute(array($username,$password));
		//echo $username.'<hr/>'.$password;
		$row2=$stmt2->fetch();
		if($row2['status']==1){
			echo "<script>window.location.href='../view/main.php';</script>";
		}else{
			echo'<script language="JavaScript">window.alert("账号或密码错误"),location.href="../view/login.php";</script>';
		}
	}
	else if($identify=='工作人员'){
		$pdo=new PDO('mysql:host=localhost;dbname=db_pet','root','root');
		$sql3="select * from ".$table." where username=? and password=?";
		$stmt3=$pdo->prepare($sql3);
		$stmt3->execute(array($username,$password));
		//echo $username.'<hr/>'.$password;
		$row3=$stmt3->fetch();
		if($row3['status']==1){
			echo "<script>window.location.href='../view/main.php';</script>";
		}else{
			echo'<script language="JavaScript">window.alert("账号或密码错误"),location.href="../view/login.php";</script>';
		}
	}
	else if($identify=='兽医'){
		echo '兽医';
		$pdo=new PDO('mysql:host=localhost;dbname=db_pet','root','root');
		$sql4="select * from ".$table." where username=? and password=?";
		$stmt4=$pdo->prepare($sql4);
		$stmt4->execute(array($username,$password));
		//echo $username.'<hr/>'.$password;
		$row4=$stmt4->fetch();
		if($row4['status']==1){
			echo "<script>window.location.href='../view/main.php';</script>";
		}else{
			echo'<script language="JavaScript">window.alert("账号或密码错误"),location.href="../view/login.php";</script>';
		}
	}
}elseif($act==='seek'){
	try{
	$pdo=new PDO('mysql:host=localhost;dbname=db_pet','root','root');
	$sql="select * from projector where email=?";
	$stmt=$pdo->prepare($sql);
	$stmt->execute(array($email));

	while($row = $stmt->fetch()){
		$username1=$row['username'];
	$password=base64_decode($row['password']);
	}
		//发送邮件，以QQ邮箱为例
		//配置邮件服务器，得到传输对象
		$transport=Swift_SmtpTransport::newInstance('smtp.qq.com',25);
		//设置登陆帐号和密码
		$transport->setUsername('704568245@qq.com');
		$transport->setPassword($emailPassword);
		//得到发送邮件对象Swift_Mailer对象
		$mailer=Swift_Mailer::newInstance($transport);
		//得到邮件信息对象
		$message=Swift_Message::newInstance();
		//设置管理员的信息
		$message->setFrom(array('704568245@qq.com'=>'动物管理系统'));
		//将邮件发给谁
		$message->setTo(array($email=>'email'));
		//设置邮件主题
		$message->setSubject('密码找回邮件');
		$str=<<<EOF
		亲爱的用户{$username1}您好！感谢您使用我们网站<br/>
		这是你的密码：{$password}，请妥善保管！<br/>	
EOF;
		$message->setBody("{$str}",'text/html','utf-8');
		try{
			if($mailer->send($message)){
				echo'<script language="JavaScript">window.alert("恭喜您密码找回成功，请到邮箱获取密码"),location.href="../view/login.php";</script>';
				//echo '<meta http-equiv="refresh" content="3;url=login.php"/>';
			}else{
				echo '此账号未注册';
				echo '<meta http-equiv="refresh" content="3;url=../view/index.php#toregister"/>';
			}
		}catch(Swift_ConnectionException $e){
			echo'<script language="JavaScript">window.location.href="../view/500.php";</script>';

			//echo '邮件发送错误'.$e->getMessage();
		}
	}
	catch(PDOException $e){
	echo $e->getMessage();
	}
	
}